import React, { useEffect, useState } from 'react'
import styles from './index.module.scss'
import Tabs from '@/components/Tabs'
import { useDispatch } from 'react-redux'
import { getAllChannellList, getChannelsList } from '@/store/actions/home'
import { useSelector } from 'react-redux'
import Icon from '@/components/Icon'
import { Drawer } from 'antd-mobile'
import Channels from './components/Channels'
import ArticleList from './components/ArticleList'
import MoreAction from './components/MoreAction'
export default function Home() {
  const dispatch = useDispatch()
  // 获取频道数据
  useEffect(() => {
    dispatch(getChannelsList())
    dispatch(getAllChannellList())
  }, [dispatch])
  const tabs = useSelector((state) => state.home.userChannels)
  // 控制抽屉组件
  // 控制频道管理抽屉的显示和隐藏
  const [drawerVisible, setDrawerVisible] = useState(false)

  const onClose = () => {
    setDrawerVisible(false)
  }

  // 定义切换Tabs高亮 的状态
  const [active, setActive] = useState(0)
  // 改变高亮 的方法
  const changeActive = (index) => {
    setActive(index)
  }
  return (
    <div className={styles.root}>
      <Tabs tabs={tabs} index={active} onChange={changeActive}>
        {tabs.map((item) => (
          <ArticleList
            key={item.id}
            channelId={item.id}
            activeId={tabs[active].id}
          />
        ))}
      </Tabs>
      {/* 频道 Tab 栏右侧的两个图标按钮：搜索、频道管理 */}
      <div className="tabs-opration">
        <Icon type="iconbtn_search" />
        <Icon type="iconbtn_channel" onClick={() => setDrawerVisible(true)} />
      </div>
      {/* 频道管理抽屉 */}
      <Drawer
        className="my-drawer"
        children={''}
        sidebar={
          drawerVisible && (
            <Channels
              index={active}
              onClose={onClose}
              onChange={changeActive}
            />
          )
        }
        open={drawerVisible}
      />
      {/* 举报反馈弹出菜单 */}
      <MoreAction />
    </div>
  )
}
